package com.secsbrain.cmdb.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.secsbrain.cmdb.biz.IAppSystemService;
import com.secsbrain.cmdb.dal.mapper.AppSystemMapper;
import com.secsbrain.cmdb.dal.model.AppSystem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.secsbrain.cmdb.client.model.App;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * AppSystem 表数据服务层接口实现类
 */
@Service
public class AppSystemServiceImpl extends SuperServiceImpl<AppSystemMapper, AppSystem> implements IAppSystemService {

    /* (non-Javadoc)
     * @see IAppSystemService#queryAppSystemPageList(com.baomidou.mybatisplus.plugins.Page, AppSystem)
     */
    @Override
    public Page<AppSystem> queryAppSystemPageList(Page<AppSystem> pageBean, AppSystem params, String orderByField) {
        pageBean.setRecords(this.baseMapper.queryAppSystemPageList(pageBean, params, orderByField));
        return pageBean;
    }

    /**
     * 功能描述: <br>
     * 获取开发者或者owner的系统列表
     *
     * @param userId
     * @return
     */
    private List<AppSystem> getDevelopOrOwnerSystem(Long userId) {
        List<AppSystem> list = new ArrayList<AppSystem>();

        List<AppSystem> templist = this.baseMapper.queryByDevelopOrOwner(userId);
        for (AppSystem appSystem : templist) {
            String developIds = appSystem.getDevelopIds();
            boolean flag = false;
            if (developIds != null && developIds.indexOf(",") != -1) {
                for (String developId : developIds.split(",")) {
                    if ((userId + "").equals(developId)) {
                        flag = true;
                        continue;
                    }
                }
            }
            if (flag || userId.equals(appSystem.getOwnerId()) || (userId + "").equals(developIds)) {
                list.add(appSystem);
            }
        }
        return list;
    }

    /* (non-Javadoc)
     * @see IAppSystemService#getAppTypeMap()
     */
    @Override
    public Map<Long, List<App>> getAppTypeMap(Long userId, boolean ownerFlag) {
        Map<Long, List<App>> map = new HashMap<Long, List<App>>();

        List<AppSystem> list = this.selectList(new AppSystem());
        if (userId != null) {
            if (ownerFlag) {
                //只取owner
                AppSystem appSystemParam = new AppSystem();
                appSystemParam.setOwnerId(userId);
                list = this.selectList(appSystemParam);
            } else {
                list = this.getDevelopOrOwnerSystem(userId);
            }
        }
        for (AppSystem appSystem : list) {
            Long appTypeId = appSystem.getTypeId();
            List<App> appList = null;
            if (map.containsKey(appTypeId)) {
                appList = map.get(appTypeId);
            } else {
                appList = new ArrayList<App>();
            }
            App app = new App();
            app.setId(appSystem.getId());
            app.setName(appSystem.getName());
            appList.add(app);
            map.put(appTypeId, appList);
        }
        return map;
    }

    private List<Integer> parsePorts(String ports) {
        List<Integer> list = new ArrayList<Integer>();
        if (StringUtils.isEmpty(ports)) {
            return list;
        }
        if (ports.indexOf(",") == -1) {
            list.add(Integer.parseInt(ports));
        } else {
            String[] strArray = ports.split(",");
            for (String str : strArray) {
                list.add(Integer.parseInt(str));
            }
        }
        return list;
    }

    private List<String> parseRedis(String redis) {
        List<String> list = new ArrayList<String>();
        if (StringUtils.isEmpty(redis)) {
            return list;
        }
        if (redis.indexOf(",") == -1) {
            list.add(redis);
        } else {
            String[] strArray = redis.split(",");
            for (String str : strArray) {
                list.add(str);
            }
        }
        return list;
    }

    /* (non-Javadoc)
     * @see IAppSystemService#queryAppSystemByTypes(java.lang.String[])
     */
    @Override
    public List<AppSystem> queryAppSystemByTypes(String[] typeArray) {
        return this.baseMapper.queryAppSystemByTypes(typeArray);
    }

}